<%-- 
    Document   : post-update-action
    Created on : 27-02-2012, 21:58:02
    Author     : NHAN
--%>

<%@page import="dao.PostCategoryDAO"%>
<%@page import="dto.PostCategory"%>
<%@page import="java.util.Date"%>
<%@page import="dto.Post"%>
<%@page import="dao.PostDAO"%>
<%@page import="util.PagingUtil"%>
<%@page import="util.SharingUtil"%>
<%@page import="java.util.ArrayList"%>

<% //content,category_id,class_id ,created_by,modified_date,state
    PostDAO daoPost = new PostDAO();
    PostCategoryDAO daoPC=new PostCategoryDAO();
    /*
    if (request.getParameter("update") != null) {
        dto.Post p = new Post();
        p.setContent(request.getParameter("content"));
        p.getCategory().setId(Integer.parseInt(request.getParameter("category_id")));
        p.getCls().setId(Integer.parseInt(request.getParameter("class_id")));
        p.getAuthor().setFirstName(request.getParameter("created_by"));
        //  p.setModifiedDate(request.getParameter("modified_date"))= new Date();
        p.setState(Boolean.parseBoolean(request.getParameter("state")));
        p.setId(Integer.parseInt(request.getParameter("postid")));
        if (daoPost.update(p) > 0) {
            SharingUtil.showMessage(session, "Your change is saved");
        } else {
            SharingUtil.showMessage(session, "Error. Action failed");
        }
    }*/
    Integer postId=Integer.parseInt(request.getParameter("id"));
    Integer cateId=Integer.parseInt(request.getParameter("cateid"));    
    ArrayList<PostCategory> cateList=new ArrayList<PostCategory>();    
    Post post=new Post();
    if(request.getParameter("update")!=null){
        post.setId(postId);
        post.setContent(request.getParameter("content"));
        post.getCategory().setId(Integer.parseInt(request.getParameter("category")));
        post.setState(request.getParameter("is-public")!=null);
        post.setModifiedDate(new Date());
        post.setAuthor(SharingUtil.getUser(session));
        post.setTitle(request.getParameter("title"));
        //post.set
        if(daoPost.update(post)>0){
            SharingUtil.showMessage(session, "Your changes is saved");
        }else{
            SharingUtil.showMessage(session, "Action failed");
        }
    }
    post=daoPost.getPost(postId);
    cateList=daoPC.getAllCategories();
    request.setAttribute("post", post);
    request.setAttribute("cateList", cateList);
            
%>